package com.yjz.study.ui.activity

import android.os.Bundle
import android.text.TextUtils
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.yjz.common.base.BaseActivity
import com.yjz.study.R
import com.yjz.study.arouter.ARouterLogic
import com.yjz.study.ui.fragment.GoodsListFragment
import com.yjz.ui.view.IconFontTextView

/**
 *作用 :
 *作者 : yjz
 *时间 : 2025/3/14 09:55
 */
@Route(path = ARouterLogic.Path.GOOD_LIST)
class GoodListActivity : BaseActivity() {
    @JvmField
    @Autowired
    var categoryTitle: String = ""

    @JvmField
    @Autowired
    var categoryId: String = ""

    @JvmField
    @Autowired
    var subcategoryId: String = ""

    private val FRAGMENT_TAG = "GOODS_LIST_FRAGMENT"

    override fun getLayoutId() = R.layout.activity_goods_list

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        ARouter.getInstance().inject(this)
        findViewById<IconFontTextView>(R.id.action_back).setOnClickListener {
            finish()
        }

        val titleTv = findViewById<TextView>(R.id.category_title)
        if (!TextUtils.isEmpty(categoryTitle)) {
            titleTv.text = categoryTitle
        }
        var fragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG)
        if (fragment == null) {
            fragment = GoodsListFragment.newInstance(categoryId, subcategoryId)
        }
        val ft = supportFragmentManager.beginTransaction()
        if (!fragment.isAdded) {
            ft.add(R.id.container, fragment, FRAGMENT_TAG)
        }
        ft.show(fragment).commitNowAllowingStateLoss()
    }
}